const getModuleName = (str) => {
  return str
    .replace(/\/(\w)/g, ($0, $1) => $1)
    .replace(/-(\w)/g, ($0, $1) => $1.toUpperCase())
    .replace(/\.js/g, '')
    .replace(/\./, '');
};

export const autoExport = (context, getName, ignoreFile = 'index') => {
  return context.keys().reduce((modules, modulePath) => {
    const value = context(modulePath);
    let moduleName;

    if (getName) {
      moduleName = getName(modulePath);
    } else {
      moduleName = getModuleName(modulePath);
    }

    if (!moduleName) {
      throw new Error('store 名字生成失败');
    }

    if (moduleName !== ignoreFile) {
      modules[moduleName] = value.default;
    }
    return modules;
  }, {});
};

export function listObj2dict(list, valueKey = 'value', labelKey = 'label') {
  return list.reduce((r, c) => {
    r[c[valueKey]] = c[labelKey];
    return r;
  }, {});
}

export function dict2listObj(dict) {
  const res = [];
  for (let key of Object.keys(dict)) {
    res.push({ value: key, label: dict[key] });
  }
  return res;
}
